package com.guanyong.services;

import com.guanyong.dao.IFuncDaoMapper;
import com.guanyong.model.FuncModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mxk on 2017/6/7.
 */
@Service
public class FuncService {
    @Autowired
    private IFuncDaoMapper iFuncDaoMapper;

    //添加
    public Integer AddFunc(FuncModel funcModel){

        Integer res=iFuncDaoMapper.insert(funcModel);
        return res;
    }

    public Integer Update(FuncModel funcModel){
       return  iFuncDaoMapper.update(funcModel);
    }



    public FuncModel SlectFuncById(Integer id){
        return iFuncDaoMapper.selectByPrimaryKey(id);
    }

    public List<FuncModel> GetAllFuncList(){
        List<FuncModel> funcList=iFuncDaoMapper.selectFuncListAll();
        return  funcList;
    }

    public List<FuncModel> GetChildFuncList(Integer paretid,List<FuncModel> listAll){
        List<FuncModel> funcModels=new ArrayList<FuncModel>();
        //List<FuncModel> listAll=GetAllFuncList();
        for (FuncModel funcModel:listAll){
            if (funcModel.getfParentid()==paretid){
                funcModels.add(funcModel);
            }
        }
        return funcModels;
    }

    public List<FuncModel> GetTreeModel(){
        List<FuncModel> parentFuncList=new ArrayList<FuncModel>();
        List<FuncModel> alllist=GetAllFuncList();
        parentFuncList=GetChildFuncList(0,alllist);

        List<FuncModel> childFuncList=null;
        if (parentFuncList.size()>0){
            for (FuncModel funcModel:parentFuncList){
                childFuncList=GetChildFuncList(funcModel.getfId(),alllist);
                if (childFuncList.size()>-1){
                    funcModel.setChildren(childFuncList);
                }

            }
        }
        return  parentFuncList;
    }

    public  List<FuncModel> GetFuncModleByIdlist(String idList){
        List<FuncModel> funcModelList=new ArrayList<FuncModel>();
        funcModelList=iFuncDaoMapper.selectIdList(idList);
        List<FuncModel> parentFuncList=new ArrayList<FuncModel>();
        parentFuncList=GetChildFuncList(0,funcModelList);
        List<FuncModel> childFuncList=null;
        if (parentFuncList.size()>0){
            for (FuncModel funcModel:parentFuncList){
                childFuncList=GetChildFuncList(funcModel.getfId(),funcModelList);
                if (childFuncList.size()>-1){
                    funcModel.setChildren(childFuncList);
                }

            }
        }
        return  parentFuncList;
    }

    public  List<FuncModel> GetAllFuncModleByIdlist(String idList){
        List<FuncModel> funcModelList=new ArrayList<FuncModel>();
        funcModelList=iFuncDaoMapper.selectIdList(idList);
        return  funcModelList;
    }

}
